﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmChiTietNhomThuoc : Form
    {
        public String action;
        NhomThuoc cNT = new NhomThuoc();
        public frmChiTietNhomThuoc()
        {
            InitializeComponent();
            
        }

        private void frmChiTietNhomThuoc_Load(object sender, EventArgs e)
        {
            if (action == "VIEW" || action == "EDIT") 
            {
                if( cNT.readThongTinNhomThuoc(Session.IDNhomThuoc) == false)
                    MessageBox.Show("Không đọc được thông tin Nhóm Thuốc!");
                else 
                    bindData(); 
            }

        }

        private void rtxDacDiem_TextChanged(object sender, EventArgs e)
        {

        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "EDIT" || this.action == "ADD")
            {
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnHuy.Enabled = true;
                btnLuu.Enabled = true;
                status = true;
            }
            else if (this.action == "VIEW")
            {
                btnHuy.Enabled = false;
                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                status = false;
            }
            txtTenNhom.Enabled = status;
            txtNhomCha.Enabled = status;
            rtxDacDiem.Enabled = status;
            rtxTacDungPhu.Enabled = status;
            rtxGhiChu.Enabled = status;
            btnSelectNhomCha.Enabled = status;
            if (this.action == "ADD")
                clearForm();
        }

        public void clearForm()
        {
            txtTenNhom.Text = "";
            txtNhomCha.Text = "";
            rtxDacDiem.Text = "";
            rtxTacDungPhu.Text = "";
            rtxGhiChu.Text = "";
        }

        public void bindData()
        {
            txtMaNhom.Text = cNT.MaNhom;
            txtTenNhom.Text = cNT.TenNhom;
            txtNhomCha.Text = cNT.TenNhomCha;
            rtxDacDiem.Text = cNT.DacDiem;
            rtxTacDungPhu.Text = cNT.TacDungPhu;
            rtxGhiChu.Text = cNT.GhiChu;
        }

        public void setData()
        {
            if (action == "ADD")
                this.cNT.getMaxID();
            else if (action == "EDIT" || action == "VIEW")
                this.cNT.ID = Session.IDNhomThuoc;
            this.cNT.MaNhom = txtMaNhom.Text;
            this.cNT.TenNhom = txtTenNhom.Text;
            //this.cNT.getParentID();
            this.cNT.DacDiem = rtxDacDiem.Text;
            this.cNT.TacDungPhu = rtxTacDungPhu.Text;
            this.cNT.GhiChu = rtxGhiChu.Text;
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            setData();
            if(action == "ADD")
                this.cNT.insertNhomThuoc();
            else if(action == "EDIT")
                this.cNT.updateNhomThuoc();
            this.applyAction("VIEW");
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.applyAction("VIEW");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            applyAction("EDIT");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Nhóm Thuốc này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                setData();
                this.cNT.deleteNhomThuoc();
                this.Close();
            }
        }

     

        private void btnSelectNhomCha_Click(object sender, EventArgs e)
        {
            View.frmDSNhomThuoc a = new View.frmDSNhomThuoc();
            a.getNC = new View.frmDSNhomThuoc.getNhomCha(setNhomCha);
            a.applyAction("SEARCH");
            a.ShowDialog();
        }

        private void frmChiTietNhomThuoc_Enter(object sender, EventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if(Session.IDNhomCha > 0)
                {
                    NhomThuoc a =  new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                    txtNhomCha.Text = a.TenNhomCha;
                }
            
        }

        private void frmChiTietNhomThuoc_VisibleChanged(object sender, EventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if (Session.IDNhomCha > 0)
                {
                    NhomThuoc a = new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                    txtNhomCha.Text = "";
                }
        }

        private void frmChiTietNhomThuoc_Validating(object sender, CancelEventArgs e)
        {
            if (action == "ADD" || action == "EDIT")
                if (Session.IDNhomCha > 0)
                {
                    NhomThuoc a = new NhomThuoc();
                    a.readThongTinNhomThuoc(Session.IDNhomThuoc);
                    txtNhomCha.Text = "";
                }
        }

        public void setNhomCha(int ID, String fname)
        {
            txtNhomCha.Text = fname;
            cNT.NhomChaID = ID;
        }
    }
}
